package com.patrol.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patrol.pojo.Worker;
import com.patrol.service.WorkerService;
import com.patrol.utils.Result;
import com.patrol.utils.TableMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@CrossOrigin
@RestController
@RequestMapping("/worker")
public class WorkerController {
    @Resource
    private WorkerService workerService;

    @PostMapping("/insert")
    public Result insert(Worker worker) {
        return workerService.insert(worker) ? Result.ok() : Result.fail();
    }

    @DeleteMapping("/delete/{workerId}")
    public Result delete(@PathVariable("workerId") int workerId) {
        return workerService.delete(workerId) ? Result.ok() : Result.fail();
    }

    @PutMapping("/update")
    public Result update(Worker worker) {
        return workerService.update(worker) ? Result.ok() : Result.fail();
    }

    @GetMapping("/select/{id}")
    public Result select(@PathVariable("id") int id) {
        return Result.ok(workerService.select(id));
    }

    @GetMapping("/selectPage")
    public TableMap selectPage(long page, long limit) {
        Page<Worker> workerPage = workerService.selectPage(page, limit);
        TableMap tableMap = new TableMap();
        tableMap.setData(workerPage.getRecords());
        tableMap.setCount(workerPage.getTotal());
        return tableMap;
    }

    @GetMapping("/selectPageByCondition")
    public TableMap selectPageByCondition(String startDate, String endDate) {
        return null;
    }

    @GetMapping("/exportData/{startTime}/{endTime}")
    public void exportData(HttpServletResponse response,
                             @PathVariable("startTime") String startTime,
                             @PathVariable("endTime") String endTime) {
        try {
            workerService.exportDataByDate(response, startTime, endTime);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
